package com.lzx.reggie.controller

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.lzx.reggie.common.BaseContext
import com.lzx.reggie.common.R
import com.lzx.reggie.entity.AddressBook
import com.lzx.reggie.service.AddressBookService
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*


@RestController
@RequestMapping("/addressBook")
class AddressBookController {

    companion object {
        val log: Logger = LoggerFactory.getLogger(AddressBookController::class.java)
    }

    @Autowired
    private lateinit var addressBookService: AddressBookService

    /**
     * 新增
     */
    @PostMapping
    fun save(@RequestBody addressBook: AddressBook): R<AddressBook> {
        addressBook.userId = BaseContext.currentId
        log.info("addressBook:{}", addressBook)
        addressBookService.save(addressBook)
        return R.success(addressBook)
    }

    /**
     * 设置默认地址
     */
    @PutMapping("default")
    fun setDefault(@RequestBody addressBook: AddressBook): R<AddressBook> {
        log.info("addressBook:{}", addressBook)

        //SQL:update address_book set is_default = 0 where user_id = ?
        addressBookService.update(
            KtQueryWrapper(AddressBook::class.java).apply {
                eq(AddressBook::userId, BaseContext.currentId)
                eq(AddressBook::isDefault, 0)
            })
        addressBook.isDefault = 1
        //SQL:update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook)
        return R.success(addressBook)
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    fun get(@PathVariable id: Long): R<Any> {
        addressBookService.getById(id).run {
            return if (this != null) {
                R.success(this)
            } else {
                R.error("没有找到该对象")
            }
        }

    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    fun getDefault(): R<Any> {

        //SQL:select * from address_book where user_id = ? and is_default = 1
        addressBookService.getOne(
            KtQueryWrapper(AddressBook::class.java).apply {
                eq(AddressBook::userId, BaseContext.currentId)
                eq(AddressBook::isDefault, 1)
            }).run {
            return if (null==this) {
                R.error("没有找到该对象")
            } else {
                R.success(this)
            }
        }

    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    fun list(addressBook: AddressBook): R<List<AddressBook>> {
        addressBook.userId = BaseContext.currentId
        log.info("addressBook:{}", addressBook)

        // SQL:select * from address_book where user_id = ? order by update_time desc
        return R.success(
            addressBookService.list(
                KtQueryWrapper(AddressBook::class.java).apply {
                    // 条件构造器
                    eq(null != addressBook.userId, AddressBook::userId, addressBook.userId)
                    orderByDesc(AddressBook::updateTime)
                })
        )
    }
}
